package com.whf.android.jar.base.mvp;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.whf.android.jar.app.Latte;
import com.whf.android.jar.media.MediaManage;


/**
 * @author Administrator
 */
public abstract class BaseMvpActivity<M extends IModel, V extends IView, P extends BasePresenter<M, V>>
        extends AppCompatActivity implements BaseMvp<M, V, P> {

    protected P presenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (setLayout() instanceof Integer) {
            int rootView = (int) setLayout();
            setContentView(rootView);
        } else if (setLayout() instanceof View) {
            View rootView = (View) setLayout();
            setContentView(rootView);
        } else {
            throw new ClassCastException("type of setLayout() must be in or View!");
        }
        //创建Presenter
        presenter = createPresenter();
        if (presenter != null) {
            //将Model层注册到Presenter中
            presenter.registerModel(createModel());
            //将View层注册到Presenter中
            presenter.registerView(createView());
        }
        onBindView(savedInstanceState);
    }

    /**
     * 布局文件
     *
     * @return Object
     */
    protected abstract Object setLayout();

    /**
     * 初始化
     *
     * @param savedInstanceState:Bundle
     */
    public abstract void onBindView(@Nullable Bundle savedInstanceState);

    @Override
    protected void onStart() {
        super.onStart();
        if(Latte.isProhibitScreenshot()){
            if (MediaManage.getInstance().checkStoragePermission()) {
                MediaManage.getInstance().init(getContentResolver());
            } else {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        MediaManage.getInstance().unregister();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaManage.getInstance().unregister();
        if (presenter != null) {
            //Activity销毁时的调用，让具体实现BasePresenter中onViewDestroy()方法做出决定
            presenter.destroy();
        }
    }
}
